﻿//
// Copyright (c) 2011 HyperCrunch, Inc. http://www.hypercrunch.com 
// Author: Nik Kalyani  http://www.kalyani.com (Twitter: @techbubble)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Framework;
using System.Text;
using System.Collections.Generic;


namespace HyperCrunch.Druid.Mobile
{
    public static class Utility
    {
        public static bool IsMobileRenderable(ModuleInfo moduleInfo, ModuleMetaInfo moduleMetaInfo, UIContext uiContext)
        {
            if (File.Exists(moduleMetaInfo.AppViewFile))
                return true;
            else
                return false;
        }

        public static bool IsDefaultRenderable(ModuleInfo moduleInfo)
        {
            object controller = GetModuleBusinessController(moduleInfo);
            if ((controller != null) && (controller is ISearchable))
                return true;
            else
                return false;
        }

        public static object GetModuleBusinessController(ModuleInfo moduleInfo)
        {
            string bizController = moduleInfo.DesktopModule.BusinessControllerClass;
            if (String.IsNullOrEmpty(bizController)) return null;
            object controller = Reflection.CreateObject(bizController, bizController);
            return controller;
        }

        public static string RouteParam(string route)
        {
            return Globals.MOBILE_ROUTE_PARAMETER + "=" + route;
        }

        public static string RouteUrl(UIContext uiContext, string route)
        {
            List<string> qs = GetQueryParams(uiContext, new List<string> { Globals.MOBILE_ROUTE_PARAMETER.ToLower()});
            qs.Add(RouteParam(route));
            return DotNetNuke.Common.Globals.NavigateURL(uiContext.PortalSettings.ActiveTab.TabID, "", qs.ToArray());
        }

        public static string NavigateUrl(UIContext uiContext, string[] queryParams)
        {
            return NavigateUrl(uiContext, queryParams, new List<string>());
        }

        public static string NavigateUrl(UIContext uiContext, string[] queryParams, List<string> skipKeys)
        {
            List<string> qs = GetQueryParams(uiContext, skipKeys);
            qs.AddRange(queryParams);

            return DotNetNuke.Common.Globals.NavigateURL(uiContext.PortalSettings.ActiveTab.TabID, "", qs.ToArray());
        }

        public static string ReturnUrl(UIContext uiContext)
        {
            if (uiContext.Context.Request.Cookies[HyperCrunch.Druid.Globals.SITE_RETURN_URL_COOKIE_NAME] != null)
                return uiContext.Context.Request.Cookies[HyperCrunch.Druid.Globals.SITE_RETURN_URL_COOKIE_NAME].Value;
            else
                return String.Empty;
        }

        private static List<string> GetQueryParams(UIContext uiContext, List<string> skipKeys)
        {
            List<string> qs = new List<string>();
            foreach (string key in uiContext.Context.Request.QueryString.Keys)
            {
                if ((key.ToLower() == "tabid") || (skipKeys.Contains(key.ToLower()))) continue;
                qs.Add(key + "=" + uiContext.Context.Request.QueryString[key]);
            }

            return qs;
        }

        public static string ErrorHtmlPage(string errorText)
        {
            return "<html><body><div style=\"color:#cc0000\">" + errorText + "</div></body></html>";
        }

    }
}
